package crawler;

import java.io.IOException;
import structures.InfoPacket;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.parserapplications.StringExtractor;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.ObjectFindingVisitor;



public class Crawler extends Thread {

	CrawlerConnection connection;

	public Crawler(int port, String address) {
		connection = CrawlerConnection.getConnector(port, address);
	}

	private void parseURL(String currentURL, String phrase) {
		
		System.out.println("Crawler - przetwarzam link: " + currentURL);
		System.out.println("Crawler - szukam tekstu: " + phrase);
		
		
		InfoPacket packet = new InfoPacket();
		Parser parser = null;

		try {
			parser = new Parser(currentURL);
		} catch (ParserException e) {
			e.printStackTrace();
		}

		ObjectFindingVisitor visitor = new ObjectFindingVisitor(LinkTag.class);

		try {
			parser.visitAllNodesWith(visitor);
		} catch (ParserException e) {
			e.printStackTrace();
		}

		Node[] links = visitor.getTags();
		for (int i = 0; i < links.length; i++) {
			LinkTag linkTag = (LinkTag) links[i];
			// System.out.println (linkTag.getLink ());
			packet.getUrlList().add(linkTag.getLink());
			
			System.out.println("Crawler - znalazłem link:" + packet.getUrlList().get(packet.getUrlList().size() - 1));
			
		}

		StringExtractor extractor = new StringExtractor(currentURL);
		try {
			String a = extractor.extractStrings(false);
			packet.setContainsString(a.contains(phrase));
		} catch (ParserException e) {
			e.printStackTrace();
		}
		
		packet.setPage(currentURL);
		try {
			connection.sendInfoPacket(packet);
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	public void run() {
		while (true) {

			InfoPacket packet = null;
			try {
				packet = connection.receivePacket();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			parseURL(packet.getPage(), packet.getStringToLookFor());

		}
	}
}
